package com.machinecontroller.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.machinecontroller.R;
import com.machinecontroller.adapter.ManagerAdapter;
import com.machinecontroller.constant.UrlConstant;
import com.machinecontroller.model.ConfigModel;
import com.machinecontroller.model.ManagerModel;
import com.machinecontroller.model.ResultModel;
import com.machinecontroller.service.GuardService;
import com.machinecontroller.utils.DevUtil;
import com.machinecontroller.utils.Logger;
import com.machinecontroller.utils.SpUtil;
import com.machinecontroller.utils.ToastUtil;
import com.sunshine.retrofit.HttpBuilder;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.Error;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.machinecontroller.ui.MainActivity.APP_MANAGER;


/**
 * 设备管理页面
 */
public class ManagerActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = ManagerActivity.class.getSimpleName();
    public static final String APP_EXIT_ALL = "app_exit_all";
    private ListView mLvManager;
    private Button mBtnRegist;
    private Button mBtnTest;
    private View mViewLine;
    private Button mBtnExit;
    private ManagerAdapter mAdapter;
    private List<ManagerModel> mList = new ArrayList<>();
    private TimeThread mTimer = new TimeThread();
    private boolean exit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);
        initView();
        initData();
        mTimer.start();
    }

    private void initView() {
        mLvManager = (ListView) findViewById(R.id.lv_manager);
        mBtnRegist = (Button) findViewById(R.id.btn_regist);
        mBtnRegist.setOnClickListener(this);
        mViewLine = (View) findViewById(R.id.view_line);
        mBtnExit = (Button) findViewById(R.id.btn_exit);
        mBtnTest = (Button) findViewById(R.id.btn_test);
        mBtnTest.setOnClickListener(this);
        mBtnExit.setOnClickListener(this);
    }

    private void initData() {
        ConfigModel model = SpUtil.getConfigModel(getApplicationContext());
        mList.add(new ManagerModel
                .Builder()
                .setId(0)
                .setName("端口号：")
                .setContent(model.getPortNum())
                .setInput(false)
                .build());
        mList.add(new ManagerModel
                .Builder()
                .setId(1)
                .setName("格子柜IMEI号：")
                .setContent(DevUtil.getImei(getApplicationContext()))
                .setInput(true)
                .build());
        mList.add(new ManagerModel
                .Builder()
                .setId(2)
                .setName("注册号：")
                .setContent(model.getRegistrationId())
                .setInput(false)
                .build());
        mList.add(new ManagerModel
                .Builder()
                .setId(3)
                .setName("图片路径：")
                .setContent(model.getImgPath())
                .setInput(false)
                .build());
        mList.add(new ManagerModel
                .Builder()
                .setId(4)
                .setName("视频路径：")
                .setContent(model.getVideoPath())
                .setInput(false)
                .build());
        mList.add(new ManagerModel
                .Builder()
                .setId(5)
                .setName("是否支持视频：")
                .setContent(model.isSuportVideo() + "")
                .setInput(false)
                .build());

        mList.add(new ManagerModel
                .Builder()
                .setId(6)
                .setName("广告轮播时间：")
                .setContent(model.getViewfliperTime() + "")
                .setInput(false)
                .build());

        mList.add(new ManagerModel
                .Builder()
                .setId(7)
                .setName("服务检查时间：")
                .setContent(model.getServiceCheckTime() + "")
                .setInput(false)
                .build());

        mList.add(new ManagerModel
                .Builder()
                .setId(8)
                .setName("版本号：")
                .setContent("V" + DevUtil.getVersion(getApplicationContext()))
                .setInput(false)
                .build());
        for (ManagerModel m : mList) {
            Logger.e(TAG, m.getContent().toString());
        }
        mAdapter = new ManagerAdapter(getApplicationContext(), mList);
        mLvManager.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_test:
                startActivity(new Intent(this, SettingActivity.class));
                break;
            case R.id.btn_regist:
                setResult(RESULT_OK,new Intent(ManagerActivity.this, MainActivity.class).putExtra(APP_EXIT_ALL, true));
                finish();
                break;
            case R.id.btn_exit:
                this.finish();
                break;
            default:
                break;
        }
    }

    private long touchTime;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        touchTime = System.currentTimeMillis();
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimer != null) {
            try {
                exit = true;
                mTimer.interrupt();
                mTimer.join();
                mTimer = null;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 用于控制页面返回
     */
    private class TimeThread extends Thread {


        @Override
        public void run() {
            super.run();
            while (!exit) {
                if (touchTime - System.currentTimeMillis() > 5 * 60 * 1000) {
                    ManagerActivity.this.finish();
                }
                try {
                    sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 启动守护线程
     */
    private void startService() {
        Intent i1 = new Intent(ManagerActivity.this, GuardService.class);
        startService(i1);
    }

    /**
     * 启动守护线程
     */
    private void stopService() {
        Intent i1 = new Intent(ManagerActivity.this, GuardService.class);
        stopService(i1);
    }
}
